#include "kernel/types.h"
#include "kernel/stat.h"
#include "user/user.h"
#include "kernel/fs.h"
#define R read(0,temp,1)
char temp[1];
//	char initDir[DIRSIZ];
void showSt(struct stat st){
	printf("st:\n");
	printf("dev:  %d ",st.dev);
	printf("ino:  %d ",st.ino);
	printf("nlink:%d ",st.nlink);
	printf("size: %d ",st.size);
	printf("type: %d \n",st.type);
}
void showDirEnt(struct dirent dn){
	printf("dirent:\t");
	printf("inum: %d ",dn.inum);
	printf("name: %s\n",dn.name);
}

int fmtloc(const char *path)
{
	//	static char res[DIRSIZ+1];
	static char buf[0x10];
	char *p;

	strcpy(buf,path);
	// Find first character after last slash.
 	for(p=buf+strlen(buf); 
	p >= buf && *p != '/'; 
	p--)
		;
	p++;
	
	if(strlen(p) >= DIRSIZ)
		return DIRSIZ;// Return blank-padded name.
	else return(p-path);

	// memmove(res, p, strlen(p));
	// memset(res+strlen(p), '\0', DIRSIZ-strlen(p));
	// return res;
}

void find(const char *path,int used)
{
	static char startDir[0x100];
	static char current[0x100];
//	static 
	char nextDir[512], *p;
	int fd;
	struct dirent de;
	struct stat st;
	int statResult;

	if(used)
		printf("\n...\n");
	else {
		memset(startDir,0,(int)(sizeof(startDir)/sizeof(startDir[0])) );
		strcpy(startDir,path);
	}

	printf("path:%s\topen result...%d\t",
		path,(fd=open(path,0)));
	if(fd < 0){
		fprintf(2, "find: cannot open %s\n", path);
		R;
		return;
	}

	printf("status:%d\t",statResult=fstat(fd,&st));
	//showSt(st);
	if(statResult<0){
		fprintf(2, "find: cannot stat %s\n", path);
		close(fd);
		return;
	}

	switch(st.type){
	case T_FILE:
		printf("file:\t%s\t %d %d %l\n", 
			path+fmtloc(path), st.type, st.ino, st.size
		);
		break;

	case T_DIR:
		if(0);
		else{
			if(strlen(path) + 1 + DIRSIZ + 1 > sizeof(nextDir)){
				printf("ls: path too long\n");
				break;
			}
			else while(read(fd, &de, sizeof(de)) == sizeof(de))
			{
				memset(nextDir,0,sizeof(nextDir));
				strcpy(nextDir, path);
				p = nextDir+strlen(nextDir);
				//move p to the end of the string
				*p++ = '/';
				//showDirEnt(de);
				if(de.inum == 0){
					printf("no file under directory %s\n",path);
					continue;
				}
				strcpy(p,de.name);
			//	strcpy(nextDir,startDir);
			//	strcpy(nextDir+strlen(nextDir),de.name);
				p[strlen(p)] = 0;
				printf("stat result...%d\n",statResult=stat(nextDir, &st));
			//	showSt(st);
				if(statResult < 0){
					printf("find: cannot stat %s\n", nextDir);
					continue;
				}
			//	close(fd);
				printf("buf:%s\t",nextDir);
				printf("formatted path:%s",(p=nextDir+fmtloc(nextDir))	);
				strcpy(current,p);
				printf("(%d)\n",strlen(p));
				if(*p=='.'){
					switch(*(p+1)){
						case '\0':	case ' ':
							printf("\\loop\n");
							nextDir[strlen(startDir)]=0;
							continue;
						case '.':
						switch(*(p+2)){
							case '\0':	case ' ':
								printf("\\loop\n");
								nextDir[strlen(startDir)]=0;	
								continue;
						}break;
					}
				}	
				printf("folder:%s\t %d %d %d\n", nextDir+fmtloc(nextDir), st.type, st.ino, st.size);
				find(nextDir,1);
			}
		}
		break;
	}
	close(fd);
}

// void find(int argc, const char *argv[]){
// 	char ch[0x20];
// 	printf("copying path...\n");
// 	memcpy(ch,argv[1],(uint)0x20);
// 	lst(ch,0);
// }
int main(int argc,const char *argv[]){
	if(argc!=3){
		write(1,"find [path] [char]\n",20U);
		return 1;
	}
	find(argv[1],0);
//	find(argc,argv);
	return 0;
}
